<?php
/**
 * 后台用户控制器
 * @author yupoxiong<i@yufuping.com>
 */

namespace app\admin\controller;

use app\admin\model\OtaGroup;
use app\admin\model\OtaUser;
use app\common\model\Attachment;
use think\Request;
use app\admin\model\OtaImei;
use app\admin\validate\OtaImeiValidate;

class OtaImeiController extends Controller
{

    //列表
    public function index(Request $request, OtaImei $model)
    {

        $param = $request->param();
        if ($this->user->is_admin == 1) {
            $data = $model->scope('where', $param)
                ->paginate($this->admin['per_page'], false, ['query' => $request->get()]);
        }else{
            $data = $model->scope('where', $param)
                ->where(['ota_account'=>$this->user->account])
                ->paginate($this->admin['per_page'], false, ['query' => $request->get()]);
        }

        $page = $data->render();

        foreach ($data as $k=>$d){
            $group_name = $model->OtaGroup()->where(['id'=>$d['ota_group']])->find();
            $data[$k]['group_name'] = $group_name['group_name'];
        }

        //关键词，排序等赋值
        $this->assign($request->get());

        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }

    //添加
    public function add(Request $request, OtaImei $model, OtaImeiValidate $validate)
    {
        if ($request->isPost()) {
            $param           = $request->param();
            //处理上传
            $attachment_up_pk = new Attachment;
            $file_up_pk       = $attachment_up_pk->upload('up_pk');
            if ($file_up_pk) {
                $param['up_pk'] = $file_up_pk->url;
            } else {
                return admin_error($attachment_up_pk->getError());
            }
            $validate_result = $validate->scene('add')->check($param);
            if (!$validate_result) {
                return admin_error($validate->getError());
            }

            $result = $model::create($param);

            $url = URL_BACK;
            if (isset($param['_create']) && $param['_create'] == 1) {
                $url = URL_RELOAD;
            }

            return $result ? admin_success('添加成功', $url) : admin_error();
        }

        return $this->fetch();
    }

    //修改
    public function edit($id, Request $request, OtaImei $model, OtaImeiValidate $validate)
    {

        $data = $model::get($id);
        if ($request->isPost()) {
            $param           = $request->param();
            //处理上传
            $attachment_up_pk = new Attachment;
            $file_up_pk       = $attachment_up_pk->upload('up_pk');
            if ($file_up_pk) {
                $param['up_pk'] = $file_up_pk->url;
            } else {
                return admin_error($attachment_up_pk->getError());
            }
            $validate_result = $validate->scene('edit')->check($param);
            if (!$validate_result) {
                return admin_error($validate->getError());
            }

            $result = $data->save($param);
            return $result ? admin_success() : admin_error();
        }

        // 获取上传文件类型
        if (!empty($data['up_pk'])){
            $data['up_pk_suffix'] = explode('.',$data['up_pk'])[1];
        }

        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch('add');

    }

    // 修改分组
    public function editGroup($id, Request $request, OtaImei $model, OtaGroup $otaGroup, OtaImeiValidate $validate)
    {
        $data = $model::get($id);
        if ($request->isPost()) {
            $param           = $request->param();
            $OtaGroupRes = $otaGroup->where(['id'=>$param['ota_group']])->find();
            $param['ota_account'] = $OtaGroupRes['ota_account'];
            $validate_result = $validate->scene('editGroup')->check($param);
            if (!$validate_result) {
                return admin_error($validate->getError());
            }

            $result = $data->save($param);
            return $result ? admin_success() : admin_error();
        }

        // 获取全部分组
        $otaGroupList = $otaGroup->where(['ota_account'=>$data['ota_account']])->select();
        foreach ($otaGroupList as $k=>$d){
            $otaGroupList[$k]['ImeiCount'] = $otaGroup->OtaImei()->where(['ota_group'=>$d['id']])->count();
        }

        $this->assign([
            'data' => $data,
            'otaGroupList' => $otaGroupList
        ]);
        return $this->fetch('edit_group');
    }


    //删除
    public function del($id, OtaImei $model)
    {
        if (count($model->noDeletionId) > 0) {
            if (is_array($id)) {
                if (array_intersect($model->noDeletionId, $id)) {
                    return admin_error('ID为' . implode(',', $model->noDeletionId) . '的数据无法删除');
                }
            } else if (in_array($id, $model->noDeletionId)) {
                return admin_error('ID为' . $id . '的数据无法删除');
            }
        }

        if ($model->softDelete) {
            $result = $model->whereIn('id', $id)->useSoftDelete('delete_time', time())->delete();
        } else {
            $result = $model->whereIn('id', $id)->delete();
        }

        return $result ? admin_success('操作成功', URL_RELOAD) : admin_error();
    }

}
